package models

import (
	"fmt"
	orm2 "github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
	"weiboxs.com/new_h_glasses/utils"
)

//问诊
type Interrogations struct {
	Id                    int64
	Archives              *Archives `orm:"rel(one);null"`
	Number                string
	StartDate             string //首诊日期
	StartDoctor           string //首诊医生
	MeetDoctor            string //接诊医生
	Optometrist           string //验配师
	Hospital              string //医院
	MainSuit              string //患者主诉
	VisionDeclineDate     string //视力下降开始时间
	BeforeConclusion      string //此前结论
	BeforeConclusionSlice []string `orm:"-" json:"-"`
	BeforeTreatment       string //此前治疗方案
	BeforeTreatmentSlice  []string `orm:"-" json:"-"`
	VisionChange1         string //视力变化
	VisionChange2         string //视力变化
	VisionChange3         string //视力变化
	VisionChange4         string //视力变化
	RefractionChange1     string //屈光度变化
	RefractionChange2     string //屈光度变化
	RefractionChange3     string //屈光度变化
	RefractionChange4     string //屈光度变化
	Nearuseeye            string //用眼时间
	OutdoorActivities     string //户外活动时间
	SleepTime             string //睡眠时间
	PastHistory           string //既往史
	PastHistorySlice      []string `orm:"-" json:"-"`
	Inheritance           string //遗传因素
	InheritanceSlice      []string `orm:"-" json:"-"`
	Environment           string //环境行为因素
	EnvironmentSlice      []string `orm:"-" json:"-"`
	RefractionOd          string //屈光诊断
	RefractionOdSlice     []string `orm:"-" json:"-"`
	RefractionOs          string
	RefractionOsSlice     []string `orm:"-" json:"-"`
	AmblyopiaOd           string //弱视诊断
	AmblyopiaOdSlice      []string `orm:"-" json:"-"`
	AmblyopiaOs           string
	AmblyopiaOsSlice      []string `orm:"-" json:"-"`
	OtherConclusionOd     string //其它结论
	OtherConclusionOs     string
	Proposal              string //方案建议
	ProposalSlice         []string `orm:"-" json:"-"`
	UpdatedAt             int64
}

func (a *Interrogations) TableName() string {
	return InterrogationsTableName()
}

func InterrogationsTableName() string {
	return TableName("his_interrogations")
}

//添加
func InterrogationsSave(data *Interrogations) error {
	orm := orm2.NewOrm()
	data.BeforeConclusion = sliceToString(data.BeforeConclusionSlice)
	data.BeforeTreatment = sliceToString(data.BeforeTreatmentSlice)
	data.PastHistory = sliceToString(data.PastHistorySlice)
	data.Environment = sliceToString(data.EnvironmentSlice)
	data.RefractionOd = sliceToString(data.RefractionOdSlice)
	data.RefractionOs = sliceToString(data.RefractionOsSlice)
	data.AmblyopiaOd = sliceToString(data.AmblyopiaOdSlice)
	data.AmblyopiaOs = sliceToString(data.AmblyopiaOsSlice)
	data.Proposal = sliceToString(data.ProposalSlice)
	data.Inheritance = sliceToString(data.InheritanceSlice)

	if data.Id == 0 { //新增
		data.Number = CreateInterrogationsNumber(data.Archives.Id)
		data.UpdatedAt = time.Now().Unix()
		if _, err := orm.Insert(data); err != nil {
			return err
		}
	} else { //修改
		data.UpdatedAt = time.Now().Unix()
		if _, err := orm.Update(data, utils.NeedUpdateFields(data, "Number")...); err != nil {
			return err
		}
	}
	return nil
}

func CreateInterrogationsNumber(archid int64) string {
	return fmt.Sprintf("%s%04d", time.Now().Format("06010215"), archid)
}

//查看
func InterrogationsView(archivesId int64) *Interrogations {
	if archivesId == 0 {
		return nil
	}
	orm := orm2.NewOrm()
	value := new(Interrogations)
	err := orm.QueryTable(InterrogationsTableName()).Filter("archives_id", archivesId).One(value)
	if err != nil {
		return nil
	}

	return value
}

func sliceToString(sli []string) string {
	if len(sli) <= 0 {
		return ""
	}
	var str string = ""
	var fl string = ""

	for k, _ := range sli {
		str += fl + sli[k]
		fl = ","
	}
	return str
}

func stringToSlice(str string) []string {
	if len(str) <= 0 {
		return nil
	}

	return strings.Split(str, ",")

}
